<div class="page">
	<?php echo $this->Form->create('Emprestimo', array('class' => 'form-horizontal'));?>
	<div class="page-header">
		<div class="pull-left">
			<i class="icon-awesome-file-alt icon-large"></i>
			<span class="divider"></span>
			<div class="title">
				<h3><?php echo __('Editar Empréstimo');?></h3>
				<small><?php echo ('Preencha os dados abaixo para adicionar os equipamentos do empréstimo');?></small>
			</div>
		</div>
		<div class="pull-right text-right">
			<?php echo ($this->request->data['Emprestimo']['situacao'] == 'A') ? $this->Form->button('<i class="icon-awesome-check icon-white"></i>' . __('Concluir'), array('type' => 'submit', 'name' => 'data[Emprestimo][submit]', 'value' => 'complete', 'escape' => false, 'class' => 'btn btn-primary btn-square')) : ''; ?>
			<?php 
				if (!empty($this->request->data['Emprestimo']['situacao']) && $this->request->data['Emprestimo']['situacao'] == 'E') {
					echo $this->Js->link('<i class="icon-awesome-signin icon-white"></i>' . __('Devolver'),
						array('controller' => 'Emprestimos','action' => 'devolucao', $this->request->data['Emprestimo']['id']),
						array(
							'title'=>'Devolução',
							'escape' => false,
							'class' => 'btn btn-success btn-square',
							'update' => '#window-modal',
							'before' => $this->Js->get('#ajax-indicator')->effect('fadeIn', array('buffer' => false)),
							'complete' => $this->Js->get('#ajax-indicator')->effect('fadeOut', array('buffer' => false))
							. ' $("#window-modal").modal(); '
							. "$('#window-modal').on('hidden', function () {
								location.reload();
							});"
						));
					echo '&nbsp;';
					echo $this->Js->link('<i class="icon-awesome-refresh icon-white"></i>' . __('Renovar'),
						array('controller' => 'Emprestimos','action' => 'renovacao', $this->request->data['Emprestimo']['id']),
						array(
							'title'=>'Renovação',
							'escape' => false,
							'class' => 'btn btn-primary btn-square',
							'update' => '#window-modal',
							'before' => $this->Js->get('#ajax-indicator')->effect('fadeIn', array('buffer' => false)),
							'complete' => $this->Js->get('#ajax-indicator')->effect('fadeOut', array('buffer' => false))
							. ' $("#window-modal").modal(); '
							. "$('#window-modal').on('hidden', function () {
								location.reload();
							});"
						));
				}
			?>
			<?php echo ($this->request->data['Emprestimo']['situacao'] == 'A') ? $this->Form->button('<i class="icon-awesome-remove icon-white"></i>' . __('Cancelar'), array('type' => 'submit', 'div' => false, 'escape' => false, 'class' => 'btn btn-danger btn-square')) : ''; ?>		
			<?php echo $this->Html->link('<i class="icon-awesome-undo icon-white"></i>' . __('Voltar'), array('action' => 'index'), array('escape' => false, 'class' => 'btn btn-square')); ?>
		</div>
	</div>
	<div class="page-content">
		<p class="required">Os campos marcados com <em>*</em> são de preenchimento obrigatório.</p>
		<fieldset>
			<legend><?php echo __('Dados do Empréstimo'); ?></legend>
			<div class="row-fluid">
				<div class="control-group span9">
					<?php echo $this->Form->Label('id', 'Nº:', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('id', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span2', 'required' => false, 'error' => false, 'disabled' => 'disabled'));	?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group span9">
					<?php echo $this->Form->Label('aluno_id', 'Solicitante:', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('aluno_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false, 'disabled' => 'disabled'));	?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group span9">
					<?php echo $this->Form->Label('projeto_id', 'Projeto:', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('projeto_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false, 'disabled' => 'disabled'));	?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group span9">
					<?php echo $this->Form->Label('autorizador_id', 'Professor:', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('autorizador_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false, 'disabled' => 'disabled'));	?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group span10">
					<?php echo $this->Form->Label('periodo', 'Período:', array('class' => 'control-label'));	?>
					<div class="controls controls-row">
						<div class="input-append span5">
							<?php $this->request->data['Emprestimo']['data_inicio'] = (!empty($this->request->data['Emprestimo']['data_inicio'])) ? date('d/m/Y', strtotime($this->request->data['Emprestimo']['data_inicio'])) : ''; ?>
							<?php echo $this->Form->input('data_inicio', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'disabled' => 'disabled', 'required' => false, 'error' => false)); ?>
							<span class="add-on"><i class="icon-calendar"></i></span>
						</div>
						<div class="input-append span5">
							<?php $this->request->data['Emprestimo']['data_entrega'] = (!empty($this->request->data['Emprestimo']['data_entrega'])) ? date('d/m/Y', strtotime($this->request->data['Emprestimo']['data_entrega'])) : ''; ?>	
							<?php echo $this->Form->input('data_entrega', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'disabled' => 'disabled', 'required' => false, 'error' => false)); ?>
							<span class="add-on"><i class="icon-calendar"></i></span>
						</div>
					</div>
				</div>
			</div>
		</fieldset>
		<?php if ($this->request->data['Emprestimo']['situacao'] == 'A') { ?>
		<fieldset>
			<legend><?php echo __('Adicionar Equipamentos'); ?></legend>
			<div class="row-fluid">
				<div class="control-group required span9 <?php echo ($this->Form->isFieldError('Itensemprestimo.0.equipamento_id')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('Itensemprestimo.0.equipamento_id', 'Equipamento', array('class' => 'control-label'));	?>
					<div class="controls">
						<div class="input-append span11">
							<?php echo $this->Form->input('Itensemprestimo.0.equipamento_id', array('type' => 'text','label' => false, 'div' => false, 'class' => 'span12', 'placeholder' => 'Digite a descrição do equipamento', 'required' => false, 'error' => false)); ?>
							<?php echo $this->Html->link('<i class="icon-search icon-white"></i>', array('controller' => 'equipamentos', 'action' => 'search'), array('class' => 'btn btn-primary', 'escape' => false, 'id' => 'btn-search-equipamento', 'title' => 'Pesquisar Equipamento')); ?>
						</div>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('Itensemprestimo.0.equipamento_id', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group required span10 <?php echo ($this->Form->isFieldError('Itensemprestimo.0.quantidade')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('Itensemprestimo.0.quantidade', 'Quantidade', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('Itensemprestimo.0.quantidade', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span2', 'required' => false, 'error' => false)); ?>
						<?php echo '&nbsp;'; ?>
						<?php echo $this->Form->button('<i class="icon-plus icon-white"></i>&nbsp;' . __('Adicionar'), array('type' => 'submit', 'name' => 'data[Emprestimo][submit]', 'value' => 'item_add', 'escape' => false, 'class' => 'btn btn-primary')); ?>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('Itensemprestimo.0.quantidade', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
					</div>
				</div>
			</div>
		</fieldset>
		<?php } ?>
		<?php if (!empty($itens_emprestimos)) { ?>
		<fieldset>
		<legend><?php echo __('Lista de Itens'); ?></legend>
		<table class="table table-bordered table-striped table-condensed data-nosortable">
			<thead>
				<tr>
					<th width="8%"><?php echo __('Código'); ?> </th>
					<th><?php echo __('Descrição do equipamento'); ?></th>
					<th width="20%"><?php echo __('Localização'); ?></th>
					<th width="10%"><?php echo __('Nº Série'); ?></th>
					<th width="10%"><?php echo __('Nº Tombo'); ?></th>
					<th width="10%"><?php echo __('Quantidade'); ?></th>
					<th width="7%" class="actions"><?php echo __('Ações'); ?></th>
				</tr>
				<?php foreach ($itens_emprestimos as $key => $row) { ?>
				<tr>
					<td><?php echo h($row['Equipamento']['id']); ?> </td>
					<td><?php echo h($row['Equipamento']['descricao']); ?></td>
					<td><?php echo h($row['Equipamento']['Localizacao']['local']); ?></td>
					<td><?php echo h($row['Equipamento']['numero_serie']); ?></td>
					<td><?php echo h($row['Equipamento']['tombo']); ?></td>
					<td><?php echo h($row['Itensemprestimo']['quantidade']); ?></td>
					<td class="actions">
						<?php echo $this->Form->postLink('<i class="icon-trash icon-white"></i>', array('action' => 'delete',$row['Itensemprestimo']['id']), array('escape' => false, 'class' => 'btn btn-danger btn-mini', 'id' => 'btn-delete-item'), __('Tem certeza que deseja excluir item?', $row['Itensemprestimo']['id'])); ?>
					</td>
				</tr>
				<?php } ?>
			</thead>
			<tbody>
			</tbody>
		</table>
		</fieldset>
		<?php } ?>
	</div>
	<?php echo $this->Form->hidden('id'); ?>
	<?php echo $this->Form->hidden('aluno_id'); ?>
	<?php echo $this->Form->hidden('projeto_id'); ?>
	<?php echo $this->Form->hidden('autorizador_id'); ?>
	<?php echo $this->Form->hidden('data_inicio'); ?>
	<?php echo $this->Form->hidden('data_entrega'); ?>
	<?php echo $this->Form->end(); ?>
</div>
<?php echo $this->element('form-modal'); ?>

<script type="text/javascript">
	$(document).ready(function () {
		$('a, button').tooltip();
		$('#EmprestimoAlunoId').select2();
		$('#EmprestimoProjetoId').select2();
		$('#EmprestimoAutorizadorId').select2();

		$('#Itensemprestimo0EquipamentoId').select2({
			minimumInputLength: 1,
		    allowClear: true,
		    formatSearching: function() { return 'Procurando...'; },
		    formatNoMatches: function(term) { return 'Nenhum equipamento encontrado'; },
		    formatInputTooShort: function(term, minLength1) { return 'Digite no mínimo um caractere'},
		    ajax: {
		    	url: baseUrl + 'equipamentos/autocomplete',
		      	dataType: 'json',
		      	data: function (term, page) {
		        	return {
		          		q: term,
		          		page_limit: 20
		        	};
		      	},
		      	results: function (data, page) {
		        	return { results: data };
		      	}
			}
		});

		$('#btn-search-equipamento').click(function(event){
			event.preventDefault();
			var url = $(this).attr('href');
			var title = $(this).attr('title');
			$("#ajax-indicator").show();
			$("#form-modal .modal-body").empty();
	        $.get(url, function (data) {
	        	$('#form-modal .modal-header h4:first').text('Pesquisar Equipamento');
	        	$('#form-modal .modal-footer button:last-child').hide();
	        	$("#form-modal .modal-body").html(data);
	        	$("#form-modal").modal();  
	        })
	        .always(function(){
	        	$("#ajax-indicator").hide();
	        });
		});
			
	});
</script>